//
//  TIMLoginViewController.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/28.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMLoginViewController.h"
#import "Masonry.h"
#import "TIMApi.h"
#import "TIMCommon.h"
#import "TIMMainTabBarController.h"

@interface TIMLoginViewController ()

@property (nonatomic, strong) UITextField *nameField;
@property (nonatomic, strong) UITextField *passwordField;
@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation TIMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.nameField];
    [self.view addSubview:self.passwordField];
    [self.view addSubview:self.loginBtn];
    self.title = @"登录";
    [self setupContrains];
}

- (void)setupContrains {
    [self.nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(180);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.nameField);
        make.top.equalTo(self.nameField.mas_bottom).offset(20);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 40));
        make.center.equalTo(self.view);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma makr - Response

- (void)clickLogin {
    [TIMApi login:self.nameField.text password:self.passwordField.text completion:^(NSError *error) {
        if (error) {
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"登录失败"
                                                                            message:@"您的账号或密码错误，请重新登录！"
                                                                     preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:action];
            [self presentViewController:alert animated:NO completion:nil];
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                TIMMainTabBarController *main = [[TIMMainTabBarController alloc] init];
                [UIApplication sharedApplication].keyWindow.rootViewController = main;
            });
        }
    }];
}

#pragma mark - Getter

- (UITextField *)nameField {
    if (!_nameField) {
        _nameField = [[UITextField alloc] init];
        _nameField.placeholder = @"请输入账号";
        _nameField.text = @"1390474951";    // 1390474951  1195229568@qq.com
//        _nameField.text = @"1390474952";         // 2267753631@qq.com
//        _nameField.text = @"1390197204";
//        _nameField.text = @"1390492522";
        _nameField.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _nameField;
}

- (UITextField *)passwordField {
    if (!_passwordField) {
        _passwordField = [[UITextField alloc] init];
        _passwordField.placeholder = @"请输入密码";
        _passwordField.text = @"wddhan083";
//        _passwordField.text = @"pengqian0987";
//        _passwordField.text = @"icbc198958";
//        _passwordField.text = @"tps123456";
        _passwordField.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _passwordField;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(clickLogin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

@end
